//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class CategoryTemplateDBManager
    {
        #region Methods
        public static void DeleteCategoryTemplate(int CategoryTemplateID)
        {
            Database db = NopDataHelper.CreateConnection();
            CategoryTemplate categoryTemplate = GetByCategoryTemplateID(CategoryTemplateID);
            if (categoryTemplate != null)
            {
                DbCommand dbCommand = db.GetStoredProcCommand("Nop_CategoryTemplateDelete");
                db.AddInParameter(dbCommand, "CategoryTemplateID", DbType.Int32, CategoryTemplateID);
                int retValue = db.ExecuteNonQuery(dbCommand);
            }
        }

        public static CategoryTemplateCollection GetAllCategoryTemplates()
        {
            CategoryTemplateCollection categoryTemplateCollection = new CategoryTemplateCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CategoryTemplateLoadAll");
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    CategoryTemplate categoryTemplate = new CategoryTemplate();
                    categoryTemplate.CategoryTemplateID = NopDataHelper.GetInt(dataReader, "CategoryTemplateID");
                    categoryTemplate.Name = NopDataHelper.GetString(dataReader, "Name");
                    categoryTemplate.TemplatePath = NopDataHelper.GetString(dataReader, "TemplatePath");
                    categoryTemplate.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    categoryTemplate.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    categoryTemplate.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                    categoryTemplateCollection.Add(categoryTemplate);
                }
            }

            return categoryTemplateCollection;
        }

        public static CategoryTemplate GetByCategoryTemplateID(int CategoryTemplateID)
        {
            CategoryTemplate categoryTemplate = null;
            if (CategoryTemplateID == 0)
                return categoryTemplate;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CategoryTemplateLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "CategoryTemplateID", DbType.Int32, CategoryTemplateID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    categoryTemplate = new CategoryTemplate();
                    categoryTemplate.CategoryTemplateID = NopDataHelper.GetInt(dataReader, "CategoryTemplateID");
                    categoryTemplate.Name = NopDataHelper.GetString(dataReader, "Name");
                    categoryTemplate.TemplatePath = NopDataHelper.GetString(dataReader, "TemplatePath");
                    categoryTemplate.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    categoryTemplate.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    categoryTemplate.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                }
            }
            return categoryTemplate;
        }

        public static CategoryTemplate InsertCategoryTemplate(string Name, string TemplatePath,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            CategoryTemplate categoryTemplate = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CategoryTemplateInsert");
            db.AddOutParameter(dbCommand, "CategoryTemplateID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "TemplatePath", DbType.String, TemplatePath);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int CategoryTemplateID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@CategoryTemplateID"));
                categoryTemplate = GetByCategoryTemplateID(CategoryTemplateID);
            }
            return categoryTemplate;
        }

        public static CategoryTemplate UpdateCategoryTemplate(int CategoryTemplateID, string Name, string TemplatePath,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            CategoryTemplate categoryTemplate = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CategoryTemplateUpdate");
            db.AddInParameter(dbCommand, "CategoryTemplateID", DbType.Int32, CategoryTemplateID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "TemplatePath", DbType.String, TemplatePath);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                categoryTemplate = GetByCategoryTemplateID(CategoryTemplateID);


            return categoryTemplate;
        }
        #endregion
    }
}
